package cn.com.inossem.operation;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import cn.com.inossem.config.WSConfig;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.LoginresultData;
import cn.com.inossem.model.RetrieveInfoBean;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;
import cn.com.inossem.utils.Utils;

public class LoginOperation implements Operation {
	public static final String LOGIN_OPERATION_USERNAME = "login_operation_username";
	public static final String LOGIN_OPERATION_PASSWORD = "login_operation_password";
	public static Bundle bundle;

	Context content = null;

	@Override
	public Bundle execute(Context arg0, Request request)
			throws ConnectionException, DataException, CustomRequestException {
		content = arg0;

		String uName = request.getString(LOGIN_OPERATION_USERNAME);
		String uPWD = request.getString(LOGIN_OPERATION_PASSWORD);

		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "login/userInfo");
		connection.setPostText("{\"username\":\"" + uName
				+ "\",\"password\":\"" + uPWD + "\",\"equipType\":\"" + "1"
				+ "\",\"equipID\":\"" + Utils.equipID + "\"}");
		System.out.println("{\"username\":\"" + uName + "\",\"password\":\""
				+ uPWD + "\",\"equipType\":\"" + "1" + "\",\"equipID\":\""
				+ Utils.equipID + "\"}");
		String body = connection.execute();

		System.out.println("LoginOperation:" + body);
		bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");

			if (errorFlag == WSConfig.RESULT_SUCESS) {

				Gson datagson = new Gson();
				LoginresultData loginresultdata = datagson.fromJson(body, LoginresultData.class);
				int isPreferences = jsonObject.getInt("isPreferences");
				if (isPreferences == 1) {
					String preferencesStr = jsonObject.get("preferences")
							.toString();
					System.out.println("preferencesStr：" + preferencesStr);
					RetrieveInfoBean preferences = datagson.fromJson(
							preferencesStr, RetrieveInfoBean.class);
					bundle.putParcelable(
							WooRequestFactory.PREFERENCES_SETING_DATA,
							preferences);
				} else {
					ContentValues Values = new ContentValues();
					Values.put("isselected", 0);
					charu("t_dictionary", Values,
							"pid=126 or pid=127 or pid=109 or pid=108");
					charu("t_areas", Values, "");
				}
				// 返回登陆的返回值
				bundle.putParcelable(WooRequestFactory.RESPONSE_LOGIN_DATA,
						loginresultdata);

				// 设置登陆结果
				bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN, errorFlag);
			} else {
				bundle.putInt(WooRequestFactory.BUNDLE_EXTRA_LOGIN, errorFlag);
				// int result = jsonObject.getInt("result");
				// String errmessage = data.getErrormsg();
				// bundle.putString(WooRequestFactory.RESPONSE_ERROR_MESSAGE,
				// errmessage);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return bundle;
	}

	public void charu(String tabname, ContentValues contentValues,
			String whereClause) {
		DatabaseHelper dh = new DatabaseHelper(content);
		SQLiteDatabase db = dh.getWritableDatabase();

		db.update(tabname, contentValues, whereClause, null);

		db.close();
		dh.close();
	}
}
